package com.example.filecollectionsystemdemo1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.filecollectionsystemdemo1.common.AuthAccess;
import com.example.filecollectionsystemdemo1.common.Result;
import com.example.filecollectionsystemdemo1.entity.Classes;
import com.example.filecollectionsystemdemo1.entity.Folders;
import com.example.filecollectionsystemdemo1.service.ClassesService;
import com.example.filecollectionsystemdemo1.service.FoldersService;
import com.example.filecollectionsystemdemo1.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Folders)表控制层
 *
 * @author makejava
 * @since 2024-10-08 17:01:51
 */
@CrossOrigin
@RestController
@RequestMapping("folders")
public class FoldersController {

    @Autowired
    FoldersService foldersService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Folders> foldersList = foldersService.list(new QueryWrapper<Folders>().orderByDesc("FolderID"));
        return Result.success(foldersList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Folders folders) {
        try {
            foldersService.save(folders);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody Folders folders) {
        foldersService.updateById(folders);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        foldersService.removeById(id);
        return Result.success();
    }

    /**
     * 多条件模糊查询信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @AuthAccess
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String folderName) {
        QueryWrapper<Folders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("FolderName", folderName);
        Page<Folders> page = foldersService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }


    @Autowired
    UsersService usersService;
    //根据当前用户班级ID查询文件夹信息
    @GetMapping("/folderByCurrentClass")
    public Result selectByClassId() {
        Integer classid = usersService.getCurrentUser().getClassid();
        QueryWrapper<Folders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ClassID", classid);
        List<Folders> foldersList = foldersService.list(queryWrapper);
        return Result.success(foldersList);
    }

}


